import React, { Component } from 'react'
import Header from '../../components/Header/Header'
import { reqgetbanner, reqgetgroupon, reqgetindexgoods, reqgetnew, reqgetseckill } from '../../http/api'
import Banner from './components/Banner/Banner'
import New from './components/New/New'
import Groupon from './components/Groupon/Groupon'
import Seckill from './components/Seckill/Seckill'
import IndexGoods from './components/IndexGoods/IndexGoods'

export class Home extends Component {
  constructor() {
    super()
    this.state = {
      banner:[],
      newList: [],
      groupon: [],
      seckill: [],
      indexGoods: [],
      n:0
    }
  }
  componentDidMount() {
    this.getbanner()
    this.getnew()
    this.getgroupon()
    this.getseckill()
    this.getindexgoods(this.state.n)
  }
  async getbanner(){
    let res = await reqgetbanner()
    if(res.data.code===200){
      this.setState({
        banner:res.data.list
      })
    }
  }
  async getnew(){
    let res = await reqgetnew()
    if (res.data.code === 200) {
      this.setState({
        newList: res.data.list
      })
    }
  }
  async getgroupon(){
    let res = await reqgetgroupon()
    if (res.data.code === 200) {
      this.setState({
        groupon: res.data.list
      })
    }
  }
  async getseckill(){
    let res = await reqgetseckill()
    if (res.data.code === 200) {
      this.setState({
        seckill: res.data.list
      })
    }
  }
  async getindexgoods(n){
    let res = await reqgetindexgoods();
    if(res.data.code===200){
      this.setState({
        indexGoods:res.data.list[n].content
      })
    }
  }
  changen(a){
    this.setState({
      n:a
    })
    this.getindexgoods(a)
  }
  render() {
    let {banner,newList,groupon,seckill,indexGoods} = this.state;
    return (
      <div>
        <Header title="首页"></Header>
        <Banner banner={banner}></Banner>
        <New newList={newList}></New>
        <Groupon groupon={groupon}></Groupon>
        <Seckill seckill={seckill}></Seckill>
        <IndexGoods indexGoods={indexGoods} changen={(a)=>this.changen(a)}></IndexGoods>
      </div>
    )
  }
}

export default Home